package ecom.beans;

import java.util.Collection;
import java.util.ArrayList;
import javax.ejb.Local;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;

@Stateful(mappedName="ecom.beans.cart")
public class CartBean implements CartRemote,CartLocal{
    // to be completed
    @PersistenceContext
    private EntityManager           manager;
    private ProductStoreBean        productStore;
    private AccountBean             clientAccount;
    private double                  totalPrice;
    private int                     state;
    private Collection<ProductBean> products;
    
    private static final int        EMPTY = 0,FILLING = 1,BOUGHT = 2;
    
    public CartBean(){
    }
    public EntityManager getManager(){return this.manager;}
    public void setManager(EntityManager em){this.manager = em;}
    
    public void setClientAccount(AccountBean acc){this.clientAccount = acc;}
    public AccountBean getClientAccount(){return this.clientAccount;}
    
    private void setState(int state){this.state = state;}
    public int getState(){return this.state;}

    public Collection<ProductBean> getProducts(){return this.products;}
    public void setProducts(Collection<ProductBean> items){this.products = items;}
    
    public double getTotalPrice(){return this.totalPrice;}
    public void setTotalPrice(double price){this.totalPrice = price;}
    
    private void updateState(){
	if(this.state != BOUGHT){
	    if(this.products.size() == 0)
		this.state = EMPTY;
	    else
		this.state = FILLING;
	}
    }
    public void addProduct(int productPK) throws CartException {
	// TODO Auto-generated method stub
	if(this.state != BOUGHT){
	    this.products.add(findProduct(productPK));
	    this.updateState();
	}
	else throw new CartException("Cart contents already bought!");
    }
    public void remProduct(int productPK) throws CartException{
	boolean removed = this.products.remove(findProduct(productPK));
	if(!removed){
	    throw new CartException("not in cart");
	}
	this.updateState();
    }
    
    public void buy(AccountBean account) throws CartException {
	// TODO Auto-generated method stub
	if(this.state != EMPTY){
	    double total = 0;
	    for(ProductBean p:this.products){
		total+=p.getPrice();
	    }
	    AccountBean accClient = this.clientAccount;
	    AccountBean accStore  = this.findAccount(this.productStore.getAccount());
	    if(accClient.getBalance() < total){
		throw new CartException("balance insuficient");
	    }
	    try{
		accStore.deposit(accClient.withdraw(total));
	    }catch(AccountException e){e.printStackTrace();}
	    for(ProductBean p:this.products){
		productStore.remProduct(p); 
	    }
	    this.setState(BOUGHT);
	     
	}
	else throw new CartException("Cart Empty");
    }
    
    public ProductBean findProduct(int id){
	return manager.find(ProductBean.class,id);
    }
    public AccountBean findAccount(int id){
	return manager.find(AccountBean.class,id);
    }
    @PostConstruct
    public void initialize(){
	this.products = new ArrayList<ProductBean>();
    }
    @PreDestroy
	public void exit(){
	System.out.println("sauvegarde des articles dans la bases");
    }
    
    
}

